package com.leyou.auth.service.impl;

import com.leyou.auth.constants.JwtConstants;
import com.leyou.auth.dto.Payload;
import com.leyou.auth.dto.UserDetail;
import com.leyou.auth.service.UserAuthService;
import com.leyou.auth.utils.JwtUtils;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.CookieUtils;
import com.leyou.user.client.UserClient;
import com.leyou.user.dto.UserDTO;
import feign.FeignException;
import feign.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.leyou.auth.constants.JwtConstants.DOMAIN;
import static com.leyou.auth.constants.JwtConstants.KEY_PREFIX;

@Service
public class UserAuthServiceImpl implements UserAuthService {

    @Autowired
    private UserClient userClient;

    @Autowired
    private JwtUtils jwtUtils;

    @Autowired
    private StringRedisTemplate redisTemplate;


    @Override
    public void login(String username, String password, HttpServletResponse response) {
/*
        //用用户名和密码校验数据库是否有数据
        UserDTO user = userClient.queryByUnameAndPwd(username, password);
       //登录校验失败
        if (user == null) {
            throw new LyException(400, "用户名或者密码错误");
        }*/


        UserDTO user = null;
        try {
            user = this.userClient.queryByUnameAndPwd(username, password);
        } catch (FeignException e) {
            new LyException(e.status(), e.getMessage());
            //可以把异常原滋原味的抛出;方便前端排错
        }


        //如果正常 生成jwt凭证
        //准备用户信息; 生成jwt
        String token = jwtUtils.createJwt(UserDetail.of(user.getId(), user.getUsername()));

        //吧jwt写入用户cookie
        Cookie cookie = new Cookie("LY_TOKEN", token);
        //绑定的域名
        cookie.setDomain("leyou.com");
        //当前域下的适配路径
        cookie.setPath("/");
        //对前端的script技术隐藏,提升安全性
        cookie.setHttpOnly(true);
        //返回cookie到浏览器
        response.addCookie(cookie);


    }

    //todo 退出登录
    @Override
    public void logout(HttpServletRequest request, HttpServletResponse response) {

        try {
            //获取cookie中的token
            String token = CookieUtils.getCookieValue(request, JwtConstants.COOKIE_NAME);
            //根据token解析信息
            Payload payload = jwtUtils.parseJwt(token);

            UserDetail userDetail = payload.getUserDetail();

            String key = KEY_PREFIX + userDetail.getUsername();

          //  if (redisTemplate.hasKey(key)){//如果redis中有这个key 就删除,同时也就退出登录了
                redisTemplate.delete(key);
           // }
        } catch (Exception e) {

        }

        //重新 生成cookie  覆盖之前的 置空
        Cookie cookie = new Cookie(JwtConstants.COOKIE_NAME,"");
        cookie.setDomain(DOMAIN);
        cookie.setPath("/");
        cookie.setMaxAge(0);
        response.addCookie(cookie);


    }


}
